using System;
using System.Threading.Tasks;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WMSSystem.Application.Contracts.Workflow.Dto;
using WMSSystem.Application.Contracts.Workflow;
using Volo.Abp.Domain.Repositories;
using WMSSystem.DYB;
using System.Linq;

namespace WMSSystem.Application.Workflow
{
    /// <summary>
    /// 创建入库工作流实例步骤
    /// </summary>
    public class CreateInboundWorkflowInstanceStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as InboundApprovalData;
            
            // 创建工作流实例记录
            data.WorkflowInstanceId = context.Workflow.Id;
            data.BusinessType = "Inbound";
            
            return ExecutionResult.Next();
        }
    }

    /// <summary>
    /// 经理审批步骤
    /// </summary>
    public class ManagerApprovalStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as InboundApprovalData;
            
            // 等待经理审批
            // 这里会等待外部事件触发
            return ExecutionResult.WaitForEvent("manager-approval", data.BusinessId.ToString(), DateTime.Now.AddDays(3));
        }
    }

    /// <summary>
    /// 财务审批步骤
    /// </summary>
    public class FinanceApprovalStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as InboundApprovalData;
            
            // 检查经理是否已审批通过
            if (!data.ManagerApproved)
            {
                return ExecutionResult.Next(); // 跳过财务审批
            }
            
            // 等待财务审批
            return ExecutionResult.WaitForEvent("finance-approval", data.BusinessId.ToString(), DateTime.Now.AddDays(3));
        }
    }

    /// <summary>
    /// 仓库管理员审批步骤
    /// </summary>
    public class WarehouseManagerApprovalStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as InboundApprovalData;
            
            // 检查财务是否已审批通过
            if (!data.FinanceApproved)
            {
                return ExecutionResult.Next(); // 跳过仓库管理员审批
            }
            
            // 等待仓库管理员审批
            return ExecutionResult.WaitForEvent("warehouse-manager-approval", data.BusinessId.ToString(), DateTime.Now.AddDays(3));
        }
    }

    /// <summary>
    /// 完成入库工作流步骤
    /// </summary>
    public class CompleteInboundWorkflowStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as InboundApprovalData;
            
            // 检查所有审批是否通过
            if (data.ManagerApproved && data.FinanceApproved && data.WarehouseManagerApproved)
            {
                // 所有审批通过，更新入库单状态为已完成
                // 这里可以添加业务逻辑，比如更新库存等
            }
            else
            {
                // 有审批被拒绝，更新入库单状态为驳回
            }
            
            return ExecutionResult.Next();
        }
    }
} 